package cn.dnui_jzy913.dnuifood_16110100913.Fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import cn.dnui_jzy913.dnuifood_16110100913.R;

public class Fragment2 extends BaseFragment {
    private TextView sc_shop,sc_food;
    private F2_FoodFragment foodFragment;
    private F2_ShopFragment shopFragment;
    @Override
    protected int initlayout() {
        return R.layout.fragment2;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (shopFragment == null){
            shopFragment = new F2_ShopFragment();
            addFragment(shopFragment);
        }else {
            replaceFragment(shopFragment);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    private void replaceFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }

    private void addFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }

    @Override
    void initViews() {
        childManager = getChildFragmentManager();
        sc_shop=view.findViewById(R.id.sc_shop);
        sc_food=view.findViewById(R.id.sc_food);
        sc_shop.setSelected(true);
        sc_shop.setTextColor(getResources().getColor(R.color.cardview_dark_background));
        sc_food.setTextColor(getResources().getColor(R.color.cardview_light_background));
    }

    @Override
    void initEvents() {
        sc_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shopFragment == null){
                    shopFragment = new F2_ShopFragment();
                }
                replaceFragment(shopFragment);
                sc_shop.setSelected(true);
                sc_food.setSelected(false);
                sc_shop.setTextColor(getResources().getColor(R.color.cardview_dark_background));
                sc_food.setTextColor(getResources().getColor(R.color.cardview_light_background));

            }
        });
        sc_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (foodFragment == null){
                    foodFragment = new F2_FoodFragment();
                }
                replaceFragment(foodFragment);
                sc_shop.setSelected(false);
                sc_food.setSelected(true);
                sc_shop.setTextColor(getResources().getColor(R.color.cardview_light_background));
                sc_food.setTextColor(getResources().getColor(R.color.cardview_dark_background));
            }
        });
    }

    @Override
    void initData() {

    }
}

